package com.hippo.core.source

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.hippo.core.model.Faq
import com.hippo.core.respository.ApiRepository

class FaqPagingSource:PagingSource<Int, Faq>() {

    override fun getRefreshKey(state: PagingState<Int, Faq>): Int? {
        return state.anchorPosition
    }


    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Faq> {
        return try {
            val position = params.key ?: 1
            val response = ApiRepository.getProductFaqList(position,10)
            LoadResult.Page(
                data = response.retData?.pageData?: emptyList(),
                prevKey = if (position == 1) null else position - 1,
                nextKey = if (response.retData?.surplusCount?:0 > 0) position + 1 else null
            )
        } catch (e: Exception) {
            LoadResult.Error(e)
        }
    }


}